// pages/home/home.js
const systemInfo = wx.getSystemInfoSync();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: systemInfo.windowHeight,
    currentTab: 'home' ,// Set the current tab to the respective page
    petList: [
      // Add more products to the list
    ],
    page: 1,
    pageSize: 10,
    total: 0,
    searchValue: '', 
  },

  onSearch(event) {
    // Handle user input in the search bar
    this.setData({
      searchValue: event.detail.value.trim(), // Trim to remove leading/trailing spaces
    });
  },

  searchPets() {
    // Implement search functionality
    if (this.data.searchValue !== '') {
      // Clear the existing petList before performing a new search
      this.setData({
        petList: [],
        page: 1,
        total: 0,
      });
  
      // Call the API to search for pets with the provided search value
      this.getPetList();
    } else {
      // If the searchValue is empty, show a message or handle it as needed
      wx.showToast({
        title: '请输入搜索关键字',
        icon: 'none',
      });
    }
  },

  selectCategory(event) {
    // Handle category selection logic here
    console.log("Category selected");
  },

  bookPet(event) {
    // Handle booking logic for the selected pet
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPetList()

  },

  getPetList(){
    wx.showLoading({
      title: '数据加载中...',
    })

    wx.request({
      url: `http://localhost:8080/admin/pet/list1`,
      method: 'Get',
      data:{
        start: this.data.page,
        pageSize: this.data.pageSize,
        // Add the keyword parameter only if searchValue is not empty
        name: this.data.searchValue,
      },
      
      // success: (res) => {
      //   this.setData({
      //     petList: [...this.data.petList, ...res.data],
      //     total: res.header['X-Total-Count'] * 1
      //   })
      // },

      success: (res) => {
        this.setData({
          petList: res.data.petList
        });
        // if (res.data && Array.isArray(res.data.message)) {
        //   this.setData({
        //     petList: [...this.data.petList, ...res.data.message],
        //     total: res.header['X-Total-Count'] * 1
        //   });
        // } else {
        //   console.error('Unexpected data format:', res.data);
        //   // Log the complete response for debugging purposes
        //   console.log('Complete response:', res);
        // }
      },            

      complete: () => {
      wx.hideLoading()
      this.setData({ isloding: false })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1,
      petList: [],
      total: 0
    })
    this.getPetList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if(this.data.petList.length == this.data.total){
      return wx.showToast({
        title: '数据加载完毕',
        icon: 'none'
      })
    }
    this.setData({
      page: this.data.page + 1
    })

    this.getPetList()  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  switchTab(event) {
    const targetTab = event.currentTarget.dataset.tab;
    if (this.data.currentTab !== targetTab) {
      wx.switchTab({
        url: `/pages/${targetTab}/${targetTab}`
      });
    }
  }
})